package com.qing.common.utils.json;

import cn.hutool.core.annotation.Alias;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSON;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;

import java.util.concurrent.atomic.AtomicBoolean;

/**
 * @author guoqf
 */
public class HutoolJsonUtil {
    public static void main(String[] args) {
//        testJsonWithBean();

//        String abcd = JSONUtil.toBean("abcd", String.class);
//        RestResponseEntity<String> en = handleFailureResponseEntity("adfadfadf", String.class);
//        System.out.println(en.getResponseData());

        testParameterizedJson();
    }

    private static void testParameterizedJson() {
     String d=   "{\n" +
                "  \"code\": \"\",\n" +
                "  \"data\": {\"id\":\"111\",\"name\":\"zhangsan\",\"my_address\":\"adfasdfasdf\"}\n" +
                "}";

        TestParameterizedJson<TestJson> json = (TestParameterizedJson<TestJson>)JSONUtil.toBean(d, TestParameterizedJson.class);
        System.out.println(json);

        JSONObject entries = new JSONObject();
    }

    private static <T> RestResponseEntity<T> handleFailureResponseEntity(String body, Class<T> failureType) {
        RestResponseEntity<T> respEntity = null;
        if (StrUtil.isNotBlank(body)) {
            respEntity = new RestResponseEntity<>();
            T f;
            if (failureType == String.class) {
                f = (T) body;
                respEntity.setResponseData(f);
            }


        }
        return respEntity;
    }

    private static void testJsonWithBean() {
        TestJson testJson = new TestJson();
        testJson.setId("111");
        testJson.setName("zhangsan");
        testJson.setMyAddress("adfasdfasdf");

        System.out.println(JSONUtil.toJsonStr(testJson));


        String str = "{\"id\":\"111\",\"name\":\"zhangsan\",\"my_address\":\"adfasdfasdf\"}";
        TestJson testJson1 = JSONUtil.toBean(str, TestJson.class);
        System.out.println(testJson1);
    }

    static class TestParameterizedJson<T>{
        private String code;

        private T data;

        public String getCode() {
            return code;
        }

        public void setCode(String code) {
            this.code = code;
        }

        public T getData() {
            return data;
        }

        public void setData(T data) {
            this.data = data;
        }
    }


    static class TestJson{
        private String id ;

        private String name;

        @Alias("my_address")
        private String myAddress;

        public String getId() {
            return id;
        }

        public void setId(String id) {
            this.id = id;
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public String getMyAddress() {
            return myAddress;
        }

        public void setMyAddress(String myAddress) {
            this.myAddress = myAddress;
        }
    }



    public static class RestResponseEntity<T> {

        private T responseData;

        public T getResponseData() {
            return responseData;
        }

        public void setResponseData(T responseData) {
            this.responseData = responseData;
        }
    }

}
